package com.xh.xyzp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xh.xyzp.common.constant.UserContext;
import com.xh.xyzp.common.domain.vo.resp.PageResp;
import com.xh.xyzp.common.enums.CommonStatusTypeEnum;
import com.xh.xyzp.domain.entity.Notice;
import com.xh.xyzp.domain.vo.NoticePageReq;
import com.xh.xyzp.domain.vo.NoticeSubmitReq;
import com.xh.xyzp.exception.ApiException;
import com.xh.xyzp.mapper.NoticeMapper;
import com.xh.xyzp.service.NoticeService;
import com.xh.xyzp.util.SnowFlakeIdWorker;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public void submit(NoticeSubmitReq req) {
        req.checkParams();
        if (Objects.isNull(req.getId())) {
            Notice notice = BeanUtil.copyProperties(req, Notice.class);
            notice.setId(new SnowFlakeIdWorker(1, 0).nextId());
            notice.setUserId(UserContext.getUser().getId());
            save(notice);
        } else {
            Notice notice = getInfo(req.getId());
            BeanUtils.copyProperties(req, notice);
            updateById(notice);
        }
    }

    @Override
    public PageResp<Notice> getList(NoticePageReq req) {
        req.checkParams();
        LambdaQueryWrapper<Notice> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Notice::getStatus, CommonStatusTypeEnum.NORMAL.getKey());
        Page<Notice> noticePage = noticeMapper.selectPage(new Page<>(req.getCurrPage(), req.getPageSize()), lambdaQueryWrapper);
        return PageResp.<Notice>builder()
                .list(noticePage.getRecords())
                .total(noticePage.getTotal())
                .build();
    }

    @Override
    public PageResp<Notice> getAdminList(NoticePageReq req) {
        req.checkParams();
        LambdaQueryWrapper<Notice> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(req.getTitle())) {
            lambdaQueryWrapper.like(Notice::getTitle, req.getTitle());
        }
        Page<Notice> noticePage = noticeMapper.selectPage(new Page<>(req.getCurrPage(), req.getPageSize()), lambdaQueryWrapper);
        return PageResp.<Notice>builder()
                .list(noticePage.getRecords())
                .total(noticePage.getTotal())
                .build();
    }

    @Override
    public Notice getInfo(Long noticeId) {
        if (Objects.isNull(noticeId)) {
            throw new ApiException("id不能为空");
        }
        return lambdaQuery()
                .eq(Notice::getId, noticeId)
                .eq(Notice::getStatus, CommonStatusTypeEnum.NORMAL.getKey())
                .one();
    }

    @Override
    public void deleteInfo(Long noticeId) {
        if (Objects.isNull(noticeId)) {
            throw new ApiException("id不能为空");
        }
        noticeMapper.deleteById(noticeId);
    }
}